<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="提现编号" prop="withdrawNo">
        <el-input
          v-model="queryParams.withdrawNo"
          placeholder="请输入提现编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号" prop="accountNo">
        <el-input
          v-model="queryParams.accountNo"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行名称" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" clearable filterable  placeholder="请选择用户类型">
          <el-option
            v-for="item in dict.type.withdraw_user_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提现类型" prop="type">
        <el-select v-model="queryParams.type" clearable filterable  placeholder="请选择提现类型">
          <el-option
            v-for="item in dict.type.commin_withd_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable filterable  placeholder="请选择状态">
          <el-option
            v-for="item in dict.type.commin_withd_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00','23:59:59']"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="提现编号" align="center" prop="withdrawNo" />
      <el-table-column label="真实姓名" align="center" prop="name" />
      <el-table-column label="用户类型" align="center" prop="userType" >
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.withdraw_user_type" :value="row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="账号" align="center" prop="accountNo" />
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="提现类型" align="center" prop="type" >
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.commin_withd_type" :value="row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center" prop="price" />
      <el-table-column label="实际到账金额" align="center" prop="realPrice" />
      <el-table-column label="提现手续费" align="center" prop="feePrice" />
      <el-table-column label="当前总佣金" align="center" prop="totalPrice" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.commin_withd_status" :value="row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(row,1)"
            v-hasPermi="['trade:brokerageWithdraw:query']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(row,2)"
            v-hasPermi="['trade:brokerageWithdraw:edit']"
          >修改</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, row)" v-if="getCaozuo(row).length"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(item,index) in getCaozuo(row)" :key="index" :command="item.command" :icon="item.icon"
              >{{item.label}}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row class="diy-text-sm diy-leading-10" :gutter="10" v-if="title == '查看详情'">
          <el-col :span="item.span || 12" v-for="(item,index) in formDetail" :key="index">
            <div class="flex_hcneter">
              <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
              <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
              </div>
              <div v-else-if="item.prop == 'auditReason' && item.isCustom">
                {{item.label}}：
                <div v-html="form[item.prop]"></div>
              </div>
              <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
            </div>
          </el-col>
        </el-row>
        <div v-else-if="title == '驳回'">
          <el-form-item label="驳回原因" prop="auditReason">
            <el-input v-model="form.auditReason" type="textarea" rows="6" clearable placeholder="请输入驳回原因"/>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="真实姓名" prop="name">
            <el-input v-model="form.name" clearable placeholder="请输入真实姓名"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="title != '查看详情'" :loading="popLodding" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBrokerageWithdraw, getBrokerageWithdraw, approve, reject,brokerageWithdrawEdit,retryTransfer, } from "@/api/finance/commiswithd";
export default {
  name: "Commiswithd",
  dicts: ['commin_withd_type','commin_withd_status','withdraw_user_type',],
  components: { },
  data() {
    return {
      popLodding:false,//弹窗加载
      formDetail:[//表单
        {label:'提现编号',prop:'withdrawNo',},{label:'真实姓名',prop:'name',},{label:'手机号',prop:'phone',},{label:'账号',prop:'accountNo',},
        {label:'银行名称',prop:'bankName',},{label:'用户类型',prop:'userType',zidian:'withdraw_user_type',isCustom:true,},
        {label:'开户地址',prop:'bankAddress',span:24,},{label:'提现金额',prop:'price',},
        {label:'实际到账金额',prop:'realPrice',},{label:'提现手续费',prop:'feePrice',},{label:'当前总佣金',prop:'totalPrice',},
        {label:'提现类型',prop:'type',zidian:'commin_withd_type',isCustom:true,},{label:'状态',prop:'status',zidian:'commin_withd_status',isCustom:true,},
        {label:'审核驳回原因',prop:'auditReason',isCustom:true,span:24,},
        {label:'审核时间',prop:'auditTime',},{label:'转账错误提示',prop:'transferErrorMsg',span:24,},
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,withdrawNo: undefined,userType: undefined,type: undefined,
        pageSize: 10,name: undefined,accountNo: undefined,bankName: undefined,
        status: undefined,dateRange: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [ { required: true, message: "真实姓名不能为空", trigger: "blur" } ],
        auditReason: [ { required: true, message: "驳回原因不能为空", trigger: "blur" } ],
      },
      shPermission:'trade:brokerageWithdraw:audit',//审核和重新转账
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getCaozuo(row){//获取操作按钮
      const user = this.$store.state.user
      const permissions = user.permissions
      const flag = !!permissions.find(item => item == this.shPermission)
      let caozuoList = []
      if( row.status === 0 && (flag || permissions[0] == '*:*:*') ) {
        caozuoList.push({label:'通过审核',command:'handleAuith',icon:'',})
        caozuoList.push({label:'驳回申请',command:'handlebohui',icon:'',})
      }
      if( row.orderStatus == 21 && (flag || permissions[0] == '*:*:*')) {
        caozuoList.push({label:'重新转账',command:'handlezuanzhag',icon:'',})
      }
      return caozuoList
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleAuith":
          this.handleAuith(row);
          break;
        case "handlebohui":
          this.handlebohui(row);
          break;
        case "handlezuanzhag":
          this.handlezuanzhag(row);
          break;
      }
    },
    /** 查列表 */
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      let dateRange = queryParams.dateRange || []
      if(dateRange.length) {
        queryParams.startTime = dateRange[0]
        queryParams.endTime = dateRange[1]
      }
      listBrokerageWithdraw(queryParams).then(response => {
        let rows = response.rows || [];
        this.list = rows.map(item=>{
          return {
            ...item,
            price:this.math.divide(item.price || 0,100),
            realPrice:this.math.divide(item.realPrice || 0,100),
            feePrice:this.math.divide(item.feePrice || 0,100),
            totalPrice:this.math.divide(item.totalPrice || 0,100),
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleDetail(row,type) {
      const id = row.id
      getBrokerageWithdraw(id).then(response => {
        let data =  response.data || {};
        this.form = {
          ...data,
          price:this.math.divide(data.price || 0,100),
          realPrice:this.math.divide(data.realPrice || 0,100),
          feePrice:this.math.divide(data.feePrice || 0,100),
          totalPrice:this.math.divide(data.totalPrice || 0,100),
        }
        this.open = true;
        this.title = type == 2 ? '修改佣金提现' : "查看详情";
      });
    },
    handlebohui(row) {//驳回
      this.open = true;
      this.title = '驳回'
      this.form = {id:row.id,}
    },
    /** 审核按钮操作 */
    handleAuith(row) {
      const id = row.id
      this.$modal.confirm(`是否确认审核通过该数据项？`).then(function() {
        return approve({id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    // 重新转账
    handlezuanzhag(row) {
      const id = row.id
      this.$modal.confirm(`是否确认重新转账该数据项？`).then(function() {
        return retryTransfer({id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          this.popLodding = true
          let api = this.title == '驳回' ? reject : brokerageWithdrawEdit
          api(form).then(response => {
            this.popLodding = false
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          }).catch(error=>this.popLodding = false);
        }
      });
    },
  }
};
</script>